<?php

require_once('ContactService.php');
require_once('GroupService.php');
require_once('UserService.php');

require_once('../lib/nusoap.php');

error_reporting(E_ERROR);

$URL = 'http://krokmitaine.free.fr/pimwi/application/service/PimwiService.php?wsdl';

/***********************************/
/** INITIALISATION DU SERVICE WEB **/
/***********************************/

$server = new soap_server();
$server->configureWSDL('PimwiService', $URL);

/***************************/
/** DECLARATION DES TYPES **/
/***************************/

// ----- User -----//

$server->wsdl->addComplexType(
  'User',
  'complexType',
  'struct',
  'all',
  '',
  array(
    'id' => array('name' => 'id', 'type' => 'xsd:integer'),
    'firstName' => array('name' => 'firstName', 'type' => 'xsd:string'),
    'lastName' => array('name' => 'lastName', 'type' => 'xsd:string'),
    'userName' => array('name' => 'userName', 'type' => 'xsd:string'),
    'password' => array('name' => 'password', 'type' => 'xsd:string')
  )
);

$server->wsdl->addComplexType(
  'ArrayOfUser',
  'complexType',
  'array',
  '',
  'SOAP-ENC:Array',
  array(),
  array(
    array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:User[]')),
  'tns:User'
);

// ----- Contact -----//

$server->wsdl->addComplexType(
  'Contact',
  'complexType',
  'struct',
  'all',
  '',
  array(
    'id' => array('name' => 'id', 'type' => 'xsd:integer'),
    'userId' => array('name' => 'userId', 'type' => 'xsd:integer'),
    'firstName' => array('name' => 'firstName', 'type' => 'xsd:string'),
    'lastName' => array('name' => 'lastName', 'type' => 'xsd:string'),
    'address' => array('name' => 'address', 'type' => 'xsd:string'),
    'city' => array('name' => 'city', 'type' => 'xsd:string'),
    'email' => array('name' => 'email', 'type' => 'xsd:string'),
    'phone' => array('name' => 'phone', 'type' => 'xsd:string'),
    'groupId' => array('name' => 'groupId', 'type' => 'xsd:integer')
  )
);

$server->wsdl->addComplexType(
  'ArrayOfContact',
  'complexType',
  'array',
  '',
  'SOAP-ENC:Array',
  array(),
  array(
    array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:Contact[]')),
  'tns:Contact'
);

// ----- Group -----//

$server->wsdl->addComplexType(
  'Group',
  'complexType',
  'struct',
  'all',
  '',
  array(
    'id' => array('name' => 'id', 'type' => 'xsd:integer'),
    'userId' => array('name' => 'userId', 'type' => 'xsd:integer'),
    'name' => array('name' => 'name', 'type' => 'xsd:string')
  )
);

$server->wsdl->addComplexType(
  'ArrayOfGroup',
  'complexType',
  'array',
  '',
  'SOAP-ENC:Array',
  array(),
  array(
    array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:Group[]')),
  'tns:Group'
);

// ----- LoginUser[Request|Response] -----//

$server->wsdl->addComplexType(
  'LoginUserRequest',
  'complexType',
  'struct',
  'all',
  '',
  array(
    'userName' => array('name' => 'userName', 'type' => 'xsd:string'),
    'password' => array('name' => 'password', 'type' => 'xsd:string')
  )
);

$server->wsdl->addComplexType(
  'LoginUserResponse',
  'complexType',
  'struct',
  'all',
  '',
  array(
    'errorMessage' => array('name' => 'errorMessage', 'type' => 'xsd:string'),
    'user' => array('name' => 'user', 'type' => 'tns:User'),
    'userContacts' => array('name' => 'userContacts', 'type' => 'tns:ArrayOfContact'),
    'userGroups' => array('name' => 'userGroups', 'type' => 'tns:ArrayOfGroup')
  )
);

// ----- SaveUser[Request|Response] -----//

$server->wsdl->addComplexType(
  'SaveUserRequest',
  'complexType',
  'struct',
  'all',
  '',
  array(
    'user' => array('name' => 'user', 'type' => 'tns:User')
  )
);

$server->wsdl->addComplexType(
  'SaveUserResponse',
  'complexType',
  'struct',
  'all',
  '',
  array(
    'errorMessage' => array('name' => 'errorMessage', 'type' => 'xsd:string')
  )
);

// ----- FindContactsByUser[Request|Response] -----//

$server->wsdl->addComplexType(
  'FindContactsByUserRequest',
  'complexType',
  'struct',
  'all',
  '',
  array(
    'userId' => array('name' => 'userId', 'type' => 'xsd:integer')
  )
);

$server->wsdl->addComplexType(
  'FindContactsByUserResponse',
  'complexType',
  'struct',
  'all',
  '',
  array(
    'errorMessage' => array('name' => 'errorMessage', 'type' => 'xsd:string'),
    'userContacts' => array('name' => 'userContacts', 'type' => 'tns:ArrayOfContact')
  )
);

// ----- SaveContact[Request|Response] -----//

$server->wsdl->addComplexType(
  'SaveContactRequest',
  'complexType',
  'struct',
  'all',
  '',
  array(
    'userId' => array('name' => 'userId', 'type' => 'xsd:integer'),
    'contact' => array('name' => 'contact', 'type' => 'tns:Contact')
  )
);

$server->wsdl->addComplexType(
  'SaveContactResponse',
  'complexType',
  'struct',
  'all',
  '',
  array(
    'errorMessage' => array('name' => 'errorMessage', 'type' => 'xsd:string'),
    'userContacts' => array('name' => 'userContacts', 'type' => 'tns:ArrayOfContact')
  )
);

// ----- DeleteContact[Request|Response] -----//

$server->wsdl->addComplexType(
  'DeleteContactRequest',
  'complexType',
  'struct',
  'all',
  '',
  array(
    'userId' => array('name' => 'userId', 'type' => 'xsd:integer'),
    'contactId' => array('name' => 'contactId', 'type' => 'xsd:integer')
  )
);

$server->wsdl->addComplexType(
  'DeleteContactResponse',
  'complexType',
  'struct',
  'all',
  '',
  array(
    'errorMessage' => array('name' => 'errorMessage', 'type' => 'xsd:string'),
    'userContacts' => array('name' => 'userContacts', 'type' => 'tns:ArrayOfContact')
  )
);

// ----- SaveGroup[Request|Response] -----//

$server->wsdl->addComplexType(
  'SaveGroupRequest',
  'complexType',
  'struct',
  'all',
  '',
  array(
    'userId' => array('name' => 'userId', 'type' => 'xsd:integer'),
    'group' => array('name' => 'group', 'type' => 'tns:Group')
  )
);

$server->wsdl->addComplexType(
  'SaveGroupResponse',
  'complexType',
  'struct',
  'all',
  '',
  array(
    'errorMessage' => array('name' => 'errorMessage', 'type' => 'xsd:string'),
    'userGroups' => array('name' => 'userGroups', 'type' => 'tns:ArrayOfGroup')
  )
);

/******************************/
/** DECLARATION DES SERVICES **/
/******************************/

// ----- User services -----//

$server->register(
  'login', 
  array('request' => 'tns:LoginUserRequest'), 
  array('response' => 'tns:LoginUserResponse'),
  $URL,
  $URL.'#login',
  'rpc',
  'encoded',
  'Connecte an utilisateur.'
);

$server->register(
  'saveUser',
  array('request' => 'tns:SaveUserRequest'), 
  array('response' => 'tns:SaveUserResponse'),
  $URL,
  $URL.'#saveUser',
  'rpc',
  'encoded',
  'Sauvegarde un utilisateur.'
);

// ----- Contact services -----//

$server->register(
  'findContactsByUser', 
  array('request' => 'tns:FindContactsByUserRequest'), 
  array('response' => 'tns:FindContactsByUserResponse'),
  $URL,
  $URL.'#findContactsByUser',
  'rpc',
  'encoded',
  'Retourne la liste de tous les contacts.'
);

$server->register(
  'saveContact',
  array('request' => 'tns:SaveContactRequest'), 
  array('response' => 'tns:SaveContactResponse'),
  $URL,
  $URL.'#saveContact',
  'rpc',
  'encoded',
  'Sauvegarde un contact.'
);

$server->register(
  'deleteContact',
  array('request' => 'tns:DeleteContactRequest'), 
  array('response' => 'tns:DeleteContactResponse'),
  $URL,
  $URL.'#deleteContact',
  'rpc',
  'encoded',
  'Supprime un contact.'
);

// ----- Group services -----//

$server->register(
  'saveGroup',
  array('request' => 'tns:SaveGroupRequest'), 
  array('response' => 'tns:SaveGroupResponse'),
  $URL,
  $URL.'#saveGroup',
  'rpc',
  'encoded',
  'Sauvegarde un groupe.'
);

/*********************************/
/** FINALISATION DU SERVICE WEB **/
/*********************************/

$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);

?>
